﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsMobile.PocketOutlook;

namespace Reminder
{
    public partial class TaskDialog : Form
    {
        const string taskPropertyName = "ReminderLocations";
        private ReminderControl owner;
        private Task task;

        public Task Task
        {
            get
            {
                return task;
            }
            set
            {
                task = value;
                LoadSubject();
                LoadLocations();
            }
        }

        public TaskDialog(ReminderControl owner)
        {
            InitializeComponent();
            this.owner = owner;
        }

        private void TaskDialog_Activated(object sender, EventArgs e)
        {
            //LoadSubject();
            //LoadLocations();
        }

        private void TaskDialog_Closing(object sender, CancelEventArgs e)
        {
            SaveTask();
        }

        private void mnuEdit_Click(object sender, EventArgs e)
        {
            SaveTask();
            Close();
            task.ShowDialog();
        }

        private void SaveTask()
        {
            task.Subject = txtSubject.Text;

            StringBuilder locationString = new StringBuilder();
            foreach (ListViewItem item in lvwLocations.Items)
            {
                if (item.Checked)
                {
                    locationString.AppendFormat("{{{0}}},",
                        ((Location)item.Tag).Guid.ToString());
                }
            }
            if (locationString.Length >= 1)
            {
                locationString.Remove(locationString.Length - 1, 1);
            }

            // Linq equivalent to the above
            /*string[] guids = lvwLocations.Items
                .OfType<ListViewItem>()
                .Where(i => i.Checked)
                .Select(i => "{" + ((Location)i.Tag).Guid.ToString() + "}")
                .ToArray();
            string locationString = string.Join(",", guids);*/

            task.Properties.Add(taskPropertyName);
            task.Properties[taskPropertyName] = locationString.ToString();
            task.Update();
        }

        private void LoadSubject()
        {
            if (txtSubject.Text != task.Subject)
            {
                txtSubject.Text = task.Subject;
            }
        }

        private void LoadLocations()
        {
            IEnumerable<Guid> guids;
            if (task.Properties.Contains(taskPropertyName) &&
                task.Properties[taskPropertyName] != null)
            {
                string locationString = task.Properties[taskPropertyName].ToString();
                guids = locationString.Split(',').Select(s => new Guid(s));
            }
            else
            {
                guids = null;
            }

            lvwLocations.Items.Clear();
            foreach (Location location in owner.Locations)
            {
                ListViewItem item = new ListViewItem(location.Name);
                item.Tag = location;
                try
                {
                    item.Checked = guids != null && guids.Contains(location.Guid);
                }
                catch { }
                lvwLocations.Items.Add(item);
            }
        }
    }
}